// ActionScript file
import healthyPointsManager.pojo.FavoriteFood;

public const CALORIE_MOD:int = 50;
public const FIBER_MOD:int = 5; 
public const FAT_MOD:int = 12;
public const FIBER_THRESHOLD:int = 4;

public const ZERO:Number = .0;
public const ONE_EIGHTH:Number = Number(1/8);
public const ONE_FOURTH:Number = Number(1/4);
public const ONE_THIRD:Number = Number(1/3);
public const THREE_EIGHTHS:Number = Number(3/8);
public const ONE_HALF:Number = Number(1/2);
public const FIVE_EIGHTHS:Number = Number(5/8);
public const TWO_THIRDS:Number = Number(2/3);
public const THREE_FOURTHS:Number = Number(3/4);
public const SEVEN_EIGHTHS:Number = Number(7/8);
public const ALMOST_WHOLE:Number = .94;

public const servingFactions:ArrayCollection = new ArrayCollection(
				[ 
			  		{label:"0", data:ZERO},
				    {label:"1/8", data:ONE_EIGHTH},
				    {label:"1/4", data:ONE_FOURTH},
				    {label:"1/3", data:ONE_THIRD},
				    {label:"3/8", data:THREE_EIGHTHS},
				    {label:"1/2", data:ONE_HALF},
				    {label:"5/8", data:FIVE_EIGHTHS},
				    {label:"2/3", data:TWO_THIRDS},
				    {label:"3/4", data:THREE_FOURTHS},
				    {label:"7/8", data:SEVEN_EIGHTHS}				    
				    
				]
			);

	/*This function takes in calories, fat, fiber and serving size and returns the point coversion per serving as a number*/
	public function calculatePoints(calories:Number, fat:Number, fiber:Number, servingSize:Number):Number{				
		if((servingSize*fiber) > FIBER_THRESHOLD){
			fiber = FIBER_THRESHOLD;
		}else{
			fiber = servingSize*fiber;
		}	
		var value:Number = (servingSize*calories)/CALORIE_MOD + ((servingSize*fat)/FAT_MOD) - fiber/FIBER_MOD;
		return formatter(value, servingSize);
	}

	//This function is responsible for formatting the points value to a whole or half number depending on the serving size
	private function formatter(value:Number, servingSize:Number):Number{
		var intValue:int = value;//remove decimal
		var newValue:Number = value - intValue;//remove number and keep only decimal value				
		if(servingSize != 1){
			if(newValue < .33){
				newValue = 0;
			}else if(newValue < .66){
				newValue = .5
			}else{
				newValue = 1;
			}
		}else{
			if(newValue < .5){
				newValue = 0;
			}else{
				newValue = 1;
			}
		}
		return intValue + newValue; 
	}
	
	//validates the given calories, fat and fiber and returns true if they can be converted to points and false otherwise
	private function validateNumbersAsPoints(calories:Number, fat:Number, fiber:Number):Boolean{
		try{
			var returnValue:Boolean = false;
			returnValue = calories >= 0 && fat >= 0 && fiber >= 0;
		}catch(err:Error){
			returnValue = false;
		}finally{
			return returnValue;
		}				
	}
	
	/*This function is called upon datagrids to accurately get the points of a favorite food to be properly displayed 
	for each line as a number based on their factual information, or points if the food is a quick add*/
	private function displayPoints(data:FavoriteFood):Number{		
		if(validateNumbersAsPoints(data.calories, data.fat, data.fiber)){
			var value:Number = calculatePoints(data.calories, data.fat, data.fiber, data.servingSize);
			if(value != (data.points)){
				data.points = value;
			}					
			return value;
		}else{//this assumes this is a quick add								 
			return data.points;
		}				
	}
		
	 //This function handles fractions of a serving size and properly returns a formatted string equivlant to the 4 main datagrids.
    private function breakDownServingSizeIntoString(value:Number):String{
		var intValue:Number = Math.floor(value);
		var fraction:String;			
		value = value - intValue;
		value = Number(value.toFixed(3));
		trace("Value after toFixed is " + value);								
		for(var i:int;i<servingFactions.length;i++){
			if(value >= Number(Number(servingFactions[i].data).toFixed(3)) && value < Number(Number(servingFactions[i+1].data).toFixed(3))){
				trace("assigning value to " + servingFactions[i].label);
				fraction = servingFactions[i].label;
				break;
			}
			if(i+2 == servingFactions.length){
				trace("Assigning value to 7/8");						
				fraction = servingFactions[i+1].label;
				break;	
			}
		}
		if(fraction == "0"){
			return intValue.toString();
		}else{
			return intValue + " & " + fraction;
		}				
	}